package com.useeinfo.oa.open.api.maintainOrder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.biz.FacilityCatBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.base.entity.FacilityCat;
import com.useeinfo.oa.modules.maintaining.biz.MaintainTechnologyBiz;
import com.useeinfo.oa.modules.maintaining.entity.MaintainTechnology;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/maintainTechnology")
public class MaintainTechnologyAPI {
    @Autowired
    private MaintainTechnologyBiz maintainTechnologyBiz;
    @Autowired
    private FacilityBiz facilityBiz;
    @Autowired
    private FacilityCatBiz facilityCatBiz;

    /**
     * 获取工艺列表
     */
    @RequestMapping(value = "/getMaintainTechnologyList")
    public ResultDto getMaintainTechnologyList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                               @RequestParam(value = "facilityId", required = false) String facilityIdParam,
                                               HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        //如果传了设备ID过来 则只查询相同设备类别的工艺
        if(StringUtils.isNotBlank(facilityIdParam)){
            Facility facility = facilityBiz.findModel(StringConverters.ToLong(facilityIdParam));
            if(facility!=null&&facility.getFacilityCat()!=null){
                queryParam.getSqlMap().put("facilityCatId",facility.getFacilityCat().getId()+"");
            }
        }

        List<MaintainTechnology> maintainTechnologyList =maintainTechnologyBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (MaintainTechnology maintainTechnology : maintainTechnologyList){
            JSONObject json = new JSONObject();
            json.put("name",maintainTechnology.getName());
            json.put("code",maintainTechnology.getCode());
            json.put("id",maintainTechnology.getId());
            //所属专业
            FacilityCat facilityCat = maintainTechnology.getFacilityCat();
            if(facilityCat!=null){
                FacilityCat rootCat = facilityCatBiz.findRootCat(facilityCat.getId());
                json.put("rootCatId",rootCat.getId());
                json.put("rootCatName",rootCat.getCatName());
            }else{
                json.put("rootCatId",null);
                json.put("rootCatName","");
            }
            array.add(json);
        }
        return ResultUtil.success(array);
    }

    /**
     * 获取工艺详情
     */
    @RequestMapping(value = "/getMaintainTechnologyDetails")
    public ResultDto getMaintainTechnologyDetails(@RequestParam(value = "id", required = false) String id,
                                                  HttpServletRequest request){
        MaintainTechnology maintainTechnology = maintainTechnologyBiz.findModel(StringConverters.ToLong(id));
        if(maintainTechnology!=null){
            return ResultUtil.success(maintainTechnology.toJSONObject());
        }else{
            return ResultUtil.error("无此工艺信息");
        }

    }
}
